package com.example.mymicaihu.fragment;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.mvplibrary.ui.fragment.BaseMvpFragment;
import com.example.mymicaihu.R;
import com.example.mymicaihu.adapter.home.MessageAdapter;
import com.example.mymicaihu.bean.MessageBean;
import com.example.mymicaihu.callback.Mainconcations;
import com.example.mymicaihu.model.MainModel;
import com.example.mymicaihu.presenter.MainPresenter;
import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.reflect.TypeToken;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;

/**
 * 作者：朱勇闯
 * data : 2021/3/29  14:30
 * 作用： xxxx
 */
public class MessageFragment<T> extends BaseMvpFragment<Mainconcations.MainView, MainModel, MainPresenter<T>> implements Mainconcations.MainView {
    @BindView(R.id.rv_list_message)
    RecyclerView rv_list;
    private static final String TAG = "朱勇闯Message";
    private List<MessageBean.DataBean> messageList;
    private MessageAdapter messageAdapter;

    @Override
    protected MainPresenter<T> createPresenter() {
        return new MainPresenter();
    }

    @Override
    protected void initEvent() {
        messageList = new ArrayList<>();
        rv_list.setLayoutManager(new LinearLayoutManager(context));
        messageAdapter = new MessageAdapter(messageList, context, R.layout.item_message);
        rv_list.setAdapter(messageAdapter);
        HashMap<String, Object> map = new HashMap<>();
        map.put("cv", "2.5.3");
//        mapTab.put("ms", "MSMyLjUuMyMyMDIwMTIwMSNtYXJrZXRfdml2byMxI3Zpdm92aXZvIFg3I2ZmZmZmZmZmLWU4ZTQtMjIzZC1mZmZmLWZmZmZlNjRiYzYzYg");
        map.put("ms", "MSMyLjUuMyMyMDIwMTIwMSNtYXJrZXRfdml2byMxI3Zpdm92aXZvIFg3I2ZmZmZmZmZmLWU4ZTQtMjIzZC1mZmZmLWZmZmZlNjRiYzYzYg==");
        map.put("referer", "http://api.micaiying.com");
        mPresenter.getData(map, "http://api.micaiying.com/", "my/msgPage", 0);
    }

    @Override
    protected int createLayoutId() {
        return R.layout.fragment_messager;
    }

    @Override
    public void showList(Object t, int type) {
        if (type == 0) {
           Gson gson = new Gson();
            Type type1 = new TypeToken<List<MessageBean.DataBean>>() {
            }.getType();
            messageList = gson.fromJson((JsonElement) t, type1);

          /*  MessageBean messageBean = new Gson().fromJson((JsonElement) t, MessageBean.class);
             = messageBean.getData();*/
            messageAdapter = new MessageAdapter(messageList, context, R.layout.item_message);
            rv_list.setAdapter(messageAdapter);
            messageAdapter.notifyDataSetChanged();
//            rv_list.addItemDecoration(new DividerItemDecoration(context, DividerItemDecoration.VERTICAL));
        }
    }

    @Override
    public void showError(String error) {

    }

    @Override
    public void showProgressbar() {

    }

    @Override
    public void hideProgressbar() {

    }
}
